package rush.rushcampus.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import rush.rushcampus.common.Result;
import rush.rushcampus.service.impl.ArticleServiceImpl;
import rush.rushcampus.service.impl.CommentServiceImpl;
import rush.rushcampus.service.impl.FoundServiceImpl;
import rush.rushcampus.service.impl.UserServiceImpl;
import rush.rushcampus.utils.QiNiuUtils;



@Slf4j
@RestController
@Api(tags = "公共接口")
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private QiNiuUtils qiNiuUtils;
    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private FoundServiceImpl foundService;
    @Autowired
    private UserServiceImpl userService;

    //图片上传接口
    @PostMapping("/upload")
    @ApiOperation("图片上传")
    public Result filesUpload(@RequestParam("fileName") MultipartFile file) {

        String filename = file.getOriginalFilename();
        //判断文件格式是否为图片
        if (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".jpeg") || filename.endsWith(".gif")|| filename.endsWith(".webp") || filename.endsWith(".svg") || filename.endsWith(".mp4")) {
            String fileUrl = qiNiuUtils.uploadImageQiniu(file);
            return Result.success(fileUrl);
        } else {
            return Result.error("文件格式不正确");
        }
    }
    ////设计各类文章详情页面部分的接口
    @GetMapping("/like")
    @ApiOperation("点赞（后续消息通知某某某对文章进行点赞）————待开发")
    public Result like(@RequestParam Long id){
        return Result.success("点赞成功");
    }
    @GetMapping("/star")
    @ApiOperation("收藏（type=1为动态，type=2为失物招领 ，isStar为0是取消）")
    @Transactional
    public Result star(@RequestParam Long id , Integer type , Integer isStar){
        //文章收藏数加减1
        if (type == 1){
            articleService.starArticle(id , isStar);
        }else if (type == 2){
            foundService.starFound(id , isStar);
        }
        //个人信息字段更新
        userService.starArticle(id ,type, isStar );
        return Result.success("收藏成功");
    }
}
